package com.tf_cut.backend.seller.service;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tf_cut.backend.mapper.GrantDetailMapper;
import com.tf_cut.backend.request.BkcloudfundsComanagementPaymentNotifyRequest;
import com.tf_cut.backend.request.CommomRequstHandle;
import com.tf_cut.backend.request.MybankNotifyService;
import com.tf_cut.backend.request.xml.XmlConverter;
import com.tf_cut.backend.seller.vo.SellerGrantModel;
import com.tf_cut.backend.seller.vo.FailMobel;
import com.tf_cut.backend.seller.vo.sellerGrantVo;
import com.tf_cut.backend.util.MyConfig;
import com.tf_cut.backend.util.RequestHead;


@Service(value = "bkcloudfundsComanagementPaymentNotifyService")
public class SelleGrantNotifyServiceImpl implements MybankNotifyService{
	
    @Autowired
    private CommomRequstHandle commomRequstHandle;
    
    @Autowired
    private SellerGrantService sellerGrantService;
    
    @Autowired
    private GrantDetailMapper grantDetailMapper;

	@Override
	public String notifyHandle(String context, String function) throws Exception {
		
		//通知内容转换
        BkcloudfundsComanagementPaymentNotifyRequest comanagementPaymentNotifyRequest =
                XmlConverter.getInstance().toResponse(context, BkcloudfundsComanagementPaymentNotifyRequest.class);
        RequestHead head = comanagementPaymentNotifyRequest.getBkcloudfundsComanagementPaymentNotify().getRequestHead();
        //通知内容保存
        boolean flag = saveNotify(comanagementPaymentNotifyRequest.getBkcloudfundsComanagementPaymentNotify().getBkcloudfundsComanagementPaymentNotifyModel());
        String jieguo = commomRequstHandle.getSignResult(flag, head);
        System.out.println("返回结果="+jieguo);
        //响应结果根据执行结果统一处理
        return jieguo;
	}
	
	
	private boolean saveNotify(SellerGrantModel model) {
		sellerGrantVo sellerGrantVo = new sellerGrantVo();
		sellerGrantVo.setId(UUID.randomUUID().toString().replace("-", ""));
		sellerGrantVo.setIsvOrgId(model.getIsvOrgId());
		sellerGrantVo.setMerchantId(model.getMerchantId());
		sellerGrantVo.setBatchNo(model.getBatchNo());
		sellerGrantVo.setRelateBatchNo(model.getRelateBatchNo());
		sellerGrantVo.setTotalAmount(model.getTotalAmount());
		sellerGrantVo.setTotalFeeAmount(model.getTotalFeeAmount());
		sellerGrantVo.setCurrency(model.getCurrency());
		sellerGrantVo.setTotalCount(model.getTotalCount());
		sellerGrantVo.setPayWay(model.getPayWay());
		sellerGrantVo.setPayrollCreditType(model.getPayrollCreditType());
		sellerGrantVo.setPayerCardNo(model.getPayerCardNo());
		sellerGrantVo.setCascadePayerId(model.getCascadePayerId());
		sellerGrantVo.setStatus(model.getStatus());
		sellerGrantVo.setFailDetailList(model.getFailDetailList());
		sellerGrantVo.setMemo(model.getMemo());
		sellerGrantVo.setNotifyType(model.getNotifyType());
		sellerGrantVo.setErrorDesc(model.getErrorDesc());
		sellerGrantVo.setAskTime(new Timestamp(System.currentTimeMillis()));
		sellerGrantService.save(sellerGrantVo);
		if(sellerGrantVo.getNotifyType().equals("BATCH_PROCESS_RESULT")){
		if(!model.getFailDetailList().equals("") && model.getFailDetailList() != null){
			String jsonList=MyConfig.Base64String(model.getFailDetailList());
			List<FailMobel> list = JSON.parseObject(jsonList,new TypeReference<List<FailMobel>>(){});
			for(int i=0;i<list.size();i++){
				grantDetailMapper.updatewangshangStatus("F",list.get(i).getOutDetailId(),list.get(i).getFailReason());
			}
		}
		grantDetailMapper.updateOrderIdStatus(model.getBatchNo());
		}
		return true;
	}

	
//	public static void main(String[] args) {
//		String json = MyConfig.Base64String("W3siT3V0RGV0YWlsSWQiOiJ0ZXN0MTIzIiwiRmFpbFJlYXNvbiI6Iua1i+ivlSJ9LHsiT3V0RGV0YWlsSWQiOiJ0ZXN0NDU2IiwiRmFpbFJlYXNvbiI6Iua1i+ivlTEifV0=");
//		List<FailMobel> list = JSON.parseObject(json,new TypeReference<List<FailMobel>>(){});
//		System.out.println(list);
//	}

}
